/*
 * @(#)AdaptLearnRate.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsl.model.algorithm;

import xfuzzy.xfsl.model.*;
import xfuzzy.lang.*;
import xfuzzy.xfds.XfdsDataSet;

/**
 * Algoritmo de factor de aprendizaje adaptativo
 * 
 * @author Francisco Jos� Moreno Velo
 * @see "Lapedes, A., Farber, R., A self-optimizing, nonsymmetrical 
 * neural net for content addressable memory and pattern recognition, 
 * Physica 22 D, pp. 247-259, 1986."
 *
 * @see "Vogl, T.P., Mangis, J.K., Rigler, A.K., Zink, W.T., Alkon, D.L.,
 *  Accelerating the Convergence of the Back-Propagation Method, 
 *  Biological Cybernetics 59, pp. 257-263, 1988."
 */
public class AdaptLearnRate extends XfslGradientBasedAlgorithm {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Factor de aprendizaje inicial
	 */
	private double initrate;
	
	/**
	 * Factor de aprendizaje actual
	 */
	private double rate;
	
	/**
	 * Factor de incremento
	 */
	private double increase;
	
	/**
	 * Factor de decremento
	 */
	private double decrease;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public AdaptLearnRate() {
		this.rate = 1.0;
		this.initrate = 1.0;
		this.increase = 1.5;
		this.decrease = 0.7;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Devuelve el c�digo de identificaci�n del algoritmo
	 */
	public int getCode() {
		return ADAPTLEARNRATE;
	}

	/**
	 * Actualiza los par�metros de configuraci�n del algoritmo
	 */
	public void setParameters(double[] param) throws XflException {
		if(param.length != 3) throw new XflException(26);
		initrate = test(param[0], POSITIVE);
		increase = test(param[1], INCREASE);
		decrease = test(param[2], DECREASE);
	}

	/**
	 * Obtiene los par�metros de configuraci�n del algoritmo
	 */
	public XfslAlgorithmParam[] getParams() {
		XfslAlgorithmParam[] pp = new XfslAlgorithmParam[3];
		pp[0] = new XfslAlgorithmParam(initrate, 1.0, POSITIVE, "Initial Learning Rate");
		pp[1] = new XfslAlgorithmParam(increase, 1.5, INCREASE, "Increase Factor");
		pp[2] = new XfslAlgorithmParam(decrease, 0.7, DECREASE, "Decrease Factor");
		return pp;
	}

	/**
	 * Ejecuta una iteraci�n del algoritmo
	 */
	public XfslEvaluation iteration(Specification spec, XfdsDataSet pattern,
			XfslErrorFunction ef) throws XflException {
		XfslEvaluation eval = null;
		Parameter[] param = spec.getAdjustable();
		if(init) { rate = initrate; init = false; } else rate *= increase;
		XfslEvaluation prev = computeErrorGradient(spec,pattern,ef);
		boolean backtracking = true;
		while(backtracking) {
			for(int i=0; i<param.length; i++) 
				param[i].setDesp(-rate*param[i].getDeriv());
			spec.update();
			eval = ef.evaluate(spec,pattern,prev.error);
			if(eval.error > prev.error) {
				rate *= decrease/increase;
				for(int i=0; i<param.length; i++) param[i].value-=param[i].getPrevDesp();
			}
			else backtracking = false;
		}

		for(int i=0; i<param.length; i++) param[i].forward();
		return eval;
	}
}

